package com.xbongbong.paas.util;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;

import java.util.List;

/**
 * @author: wufeng
 * @date: 2018/9/5 10:49
 * @desrcption: 搜索条件拼接
 */
public class SearchConditionUtil {

    /**
     * 数据列表搜索条件拼装
     * @param formDataListDTO 搜索条件
     * @return 返回拼接后的字符串
     */
    @Deprecated
    public static StringBuffer sqlStringBuffer(FormDataListDTO formDataListDTO) {
        List<ConditionsEntityExt> list = formDataListDTO.getConditions();
        String rel = formDataListDTO.getRel();
        StringBuffer stringBuffer = new StringBuffer();
        for (ConditionsEntityExt condition: list ) {
            stringBuffer = stringBuffer.append(condition(condition, rel));
        }
        return stringBuffer;
    }

    public static String condition(ConditionsEntityExt condition, String rel){
        String sqlString = "";
        ConditionEnum conditionEnum = ConditionEnum.getConditionEnum(condition.getSymbol());
        switch (conditionEnum){
            case EQUAL :
                sqlString = equal(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case NOEQUAL:
                sqlString = noEqual(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case EMPTY:
                sqlString = empty(condition.getAttr(), rel);
                break;
            case NOEMPTY:
                sqlString = noEmpty(condition.getAttr(), rel);
                break;
            case INCLUDE:
                sqlString = include(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case ALLINCLUDE:
                sqlString = allInclude(condition.getAttr(), condition.getValue(), rel);
                break;
            case NOLIKE:
                sqlString = noInclude(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case IN:
                sqlString = in(condition.getAttr(), condition.getValue(), rel);
                break;
            case NOIN:
                sqlString = noIn(condition.getAttr(), condition.getValue(), rel);
                break;
            case GREATEREQUAL:
                sqlString = greaterEqual(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case GREATERTHAN:
                sqlString = greaterThan(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case LESSEQUAL:
                sqlString = lessEqual(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case LESSTHAN:
                sqlString = lessThan(condition.getAttr(), condition.getValue().get(0), rel);
                break;
            case RANGE:
                sqlString = range(condition.getAttr(), condition.getValue(), rel);
                break;
                // TODO 地址判断暂时去掉
//            case BELONG:
//                sqlString = belong(condition.getAttr(), condition.getValue(), rel);
//                break;
//            case NOBELONG:
//                sqlString = noBelong(condition.getAttr(), condition.getValue(), rel);
//                break;
            default:
                break;
        }
        return sqlString;
    }


    /**
     * 等于
     * @param attr
     * @param value
     * @return
     */
    public static String equal(String attr, Object value, String rel){
        //and JSON_EXTRACT(data, '$."1"') = "1"
        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") = " + value;
    }

    /**
     * 不等于
     * @param attr
     * @param value
     * @return
     */
    public static String noEqual(String attr, Object value, String rel){

        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") = " + value;
    }

    /**
     * 为空
     * @param attr
     * @return
     */
    public static String empty(String attr, Object rel) {
        return " " + rel + " (JSON_EXTRACT(data, $." + attr + ") == '' || JSON_EXTRACT(data, $." + attr + ") IS NULL)";
    }

    /**
     * 不为空
     * @param attr
     * @return
     */
    public static String noEmpty(String attr, Object rel) {
        return " " + rel + " (JSON_EXTRACT(data, $." + attr + ") != '' || JSON_EXTRACT(data, $." + attr + ") IS NOT NULL)";
    }

    /**
     * 包含
     * @param attr
     * @param value
     * @return
     */
    public static String include(String attr, Object value, String rel) {
        return " " + rel + " (JSON_EXTRACT(data, $." + attr + ") LIKE %" + value + "%";
    }

    /**
     * 不包含
     * @param attr
     * @param value
     * @return
     */
    public static String noInclude(String attr, Object value, String rel) {
        return " " + rel + " (JSON_EXTRACT(data, $." + attr + ") LIKE %" + value + "%";
    }
    /**
     * 同时包含
     * @param attr
     * @param value
     * @param rel
     * @return
     */
    private static String allInclude(String attr, List<Object> value, String rel) {
        String sqlString = "";
        for (Object s: value) {
            sqlString =  equal(attr, s, "AND");
        }
        return " " + rel +"(" + sqlString + ")";
    }
    /**
     * 等于任意一个
     * @param attr
     * @param value
     * @param rel
     * @return
     */
    public static String in(String attr, List<Object> value, String rel) {
        String sqlString = "";
        for (Object s: value) {
            sqlString = equal(attr, s, "OR");
        }
        return " " + rel +"(" + sqlString + ")";
    }

    /**
     * 不等于任意一个
     * @param attr
     * @param value
     * @param rel
     * @return
     */
    public static String noIn(String attr, List<Object> value, String rel) {
        String sqlString = "";
        for (Object s: value) {
            sqlString =  noEqual(attr, s, "AND");
        }
        return " " + rel +"(" + sqlString + ")";
    }
    /**
     * 大于等于
     * @param attr
     * @param value
     * @return
     */
    public static String greaterEqual(String attr, Object value, String rel){

        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") >= " + value;
    }
    /**
     * 大于
     * @param attr
     * @param value
     * @return
     */
    public static String greaterThan(String attr, Object value, String rel){

        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") > " + value;
    }
    /**
     * 小于等于
     * @param attr
     * @param value
     * @return
     */
    public static String lessEqual(String attr, Object value, String rel){

        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") <= " + value;
    }

    /**
     * 小于等于
     * @param attr
     * @param value
     * @return
     */
    public static String lessThan(String attr, Object value, String rel){

        return " " + rel + " JSON_EXTRACT(data, $." + attr + ") <= " + value;
    }

    /**
     * 选择范围
     * @param attr
     * @param value
     * @param rel
     * @return
     */
    public static String range(String attr, List<Object> value, String rel) {

        return " " + rel + "(" + greaterEqual(attr, value.get(0), "") + lessEqual(attr, value.get(1), "AND") + ")";
    }

    /**
     * 属于
     * @param attr
     * @param list
     * @param rel
     * @return
     */
    public static String belong(String attr, List<Object> list, String rel) {
        String value = "";
        for (Object s : list) {
            value = value + s;
        }
        return equal(attr, value, rel);
    }

    /**
     * 不属于
     * @param attr
     * @param list
     * @param rel
     * @return
     */
    public static String noBelong(String attr, List<Object> list, String rel) {
        String value = "";
        for (Object s : list) {
            value = value + s;
        }
        return noEqual(attr, value, rel);
    }
}
